import modelExtend from 'dva-model-extend'
import { model } from 'models/common'
import { message } from 'antd'
import {pageArt,topArt,releaseArt,offShelve,deleteArt} from 'services/article'

export default modelExtend(model, {
    namespace:'articleList',
    state:{
       
    },
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen((location) => {
                if (location.pathname.includes('articleList')) {
                    dispatch({
                        type:'page',
                        payload:{
                            size:10,
                            start:1,
                            endTime:'',
                            startTime:'',
                            status:''
                        }
                    })
                }
            })
        },
    },
    effects:{
        //查询
        *page({payload},{call,put}){
            const data = yield call(pageArt,payload);
            if(data.code == 0){
                const allCount = data.data.allCount;
                const articleList = data.data.articleList;
                yield put({
                    type:'updateState',
                    payload:allCount
                })
                yield put({
                    type:'updateArt',
                    payload:articleList
                })
            }
        },
        //置顶操作
        *topArt({payload},{call,put}){
            const hide = message.loading('操作中...');
            const data = yield call(topArt,payload.value);
            setTimeout(hide, 0);
            if(data.code == 0){
                yield put({
                    type:'page',
                    payload:{
                        size:10,
                        start:payload.page,
                        endTime:payload.endTime,
                        startTime:payload.startTime,
                        status:payload.status
                    }
                })
                message.success(data.msg,2);
            }else{
                message.error(data.msg)
            }
        },
        //发布
        *releaseArt({payload},{call,put}){
            const data = yield call(releaseArt,payload.value);
            if(data.code == 0){
                yield put({
                    type:'page',
                    payload:{
                        size:10,
                        start:payload.page,
                        endTime:payload.endTime,
                        startTime:payload.startTime,
                        status:payload.status
                    }
                });
                message.success('发布成功！',2);
            }else{
                message.error(data.msg);
            }
        },
        //下架
        *offShelve({payload},{call,put}){
            const data = yield call(offShelve,payload.value);
            if(data.code == 0){
                yield put({
                    type:'page',
                    payload:{
                        size:10,
                        start:payload.page,
                        endTime:payload.endTime,
                        startTime:payload.startTime,
                        status:payload.status
                    }
                });
                message.success('下架成功！',2);
            }else{
                message.error(data.msg)
            }
        },
        //删除
        *deleteArt({payload},{call,put}){
            const data = yield call(deleteArt,payload.value);
            if(data.code == 0){
                yield put({
                    type:'page',
                    payload:{
                        size:10,
                        start:payload.page,
                        endTime:payload.endTime,
                        startTime:payload.startTime,
                        status:payload.status
                    }
                });
                message.success('删除成功！',2);
            }else{
                message.error(data.msg)
            }
        }

    },
    reducers:{
        updateState(state,{payload}){
            return {
                ...state,
                allCount:payload,
            }
        },
        updateArt(state,{payload}){
            return {
                ...state,
                articleList:payload,
            }
        }
    }
})